<?php
header("Content-Type:text/html;chardset=utf-8");
?>
<?php
session_start();

//include 'db.inc.php';

$MYSQL_HOST='localhost';
$MYSQL_USER='root';
$MYSQL_PASSWORD='root';
$MYSQL_DB='CreateHouse';

$db = mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD ) or 
	die('Unable to connect. Check your connection parameters.');
	mysql_query(' set names utf8;', $db) or die(mysql_error($db));
	
$register_mail = (isset($_POST['register_mail'])) ? trim($_POST['register_mail']) : '';
$register_ulike = urldecode((isset($_POST['register_ulike'])) ? trim($_POST['register_ulike']) : '');
$sex = ( isset($_POST['sex'])) ? trim($_POST['sex']) : '';
$password = (isset($_POST['register_respassword'])) ? $_POST['register_respassword'] : '';
$repassword = (isset($_POST['register_respassword'])) ? $_POST['register_respassword'] : '';


if(isset($_POST['submit']))
{
	$errors = array();
	//make sure manditory fields have been entered
	if(empty($register_mail))
	{
		$errors[] = 'register_mail cannot be blank.';
	}
	mysql_select_db($MYSQL_DB);
	
	$query = 'CREATE TABLE IF NOT EXISTS site_user (
		user_id	INTEGER NOT NULL,
		register_mail	VARCHAR(40) NOT NULL,
		register_ulike	VARCHAR(40)	NOT NULL,
		sex	VARCHAR(10)	NOT NULL,
		password	VARCHAR(80)	NOT NULL,
		
		PRIMARY KEY(user_id)
		)
		ENGINE=MyISAM';

	mysql_query($query, $db) or die(mysql_error($db));
	//check if username already is registered
	$query = 'SELECT register_mail FROM site_user WHERE register_mail = "' . $register_mail .'"';
	$result = mysql_query($query, $db) or die(mysql_error());
	if(mysql_num_rows($result) > 0)
	{
		$errors[] = 'register_mail ' . $register_mail . ' is already registered.';
		$register_mail = '';
	}
	mysql_free_result($result);

	if(empty($register_ulike))
	{
		$errors[] = 'register ulike should not be blank';
	}
	$query = 'SELECT register_ulike FROM site_user WHERE register_ulike = "' . $register_ulike . '"';
	$result = mysql_query($query, $db) or die(mysql_error($db));
	if(mysql_num_rows($result) > 0)
	{
		$errors[] = 'register_ulike ' . $register_ulike . ' is already registed.';
		$register_ulike = '';
	}
	mysql_free_result($result);

	
	
	if(empty($password))
	{
		$errors[] = 'Password cannot be blank.';
	}
	if($password !== $repassword)
	{
		$errors[] = 'password should be equal to repassword';
		$repassword = '';
	}

	if(count($errors) > 0)
	{
		echo '<p><strong style="color:#FF000;" >Unable to process your registation.</strong></p>';
		echo '<p>Please fix the following: </p>';
		echo '<ul>';
		foreach($errors as $error)
		{
			echo '<li>' . $error . '</li>';

		}
		echo '<p><strong><a href = "./../register.html">请重新输入您的注册邮箱或者昵称</strong></a></p>';

	}
	else
	{
		//No errors , so enter the information into the database

		//$user_id = mysql_insert_id($db);
		$query = 'SELECT register_mail FROM site_user';
		$result = mysql_query($query, $db) or die(mysql_error());
		$user_id = mysql_num_rows($result);

		$query = 'INSERT INTO site_user (user_id, register_mail, register_ulike, sex, password)
					VALUES (' . $user_id . ', "' . mysql_real_escape_string($register_mail, $db) . '",
					"' . mysql_real_escape_string($register_ulike, $db) . '", 
					"' . mysql_real_escape_string($_POST['sex'], $db) . '",
					MD5("' . mysql_real_escape_string($password, $db) . '"))';
		$result = mysql_query($query, $db) or die(mysql_error());

		$uesr_id = mysql_insert_id($db);
		
//		$query = 'INSERT INTO site_user_info
//					(user_id, first_name, last_name, email, city, state, hobbies)
//					VALUES
//					(' . $user_id . ',
//					' . '"' . mysql_real_escape_string($first_name, $db) . '",
//					' . '"' . mysql_real_escape_string($last_name, $db) . '",
//					' . '"' . mysql_real_escape_string($email, $db) . '",
//   				' . '"' . mysql_real_escape_string($city, $db) . '",
//					' . '"' . mysql_real_escape_string($city, $db) . '",
//					' . '"' . mysql_real_escape_string($state, $db) . '",
//					' . '"' . mysql_real_escape_string(join(', ', $hobbies), $db) . '")';
//		$result = mysql_query($query, $db) or die(mysql_error());
	
		$_SESSION['logged'] = 1;
		$_SESSION['register_mail'] = $register_mail;

		header('Refresh: 5;URL=./main/main.php');
	}
}
?>
